package com.xudm.cdg.config;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.alibaba.fastjson2.JSONObject;
import com.xudm.cdg.entity.ResponseResult;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.plantuml.Run;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/**
 * 全局异常拦截
 *
 * @author xudm
 */
@ControllerAdvice
@Slf4j
public class GlobExceptionHandler {

    @ExceptionHandler({Exception.class, RuntimeException.class, HttpMessageNotReadableException.class,
            HttpMediaTypeNotSupportedException.class, MaxUploadSizeExceededException.class,
            HttpRequestMethodNotSupportedException.class, MethodArgumentNotValidException.class, BindException.class})
    @ResponseBody
    <T> ResponseResult<T> serverExceptionHandler(HttpServletRequest request, HttpServletResponse response, Throwable e) {
        log.error("请求:{} 异常,异常信息:{}", request.getRequestURI(), ExceptionUtil.stacktraceToString(e));
        return ResponseResult.serverError(null);
    }

}
